<?php
/*
 * Wolf CMS - Content Management Simplified. <http://www.wolfcms.org>
 * Copyright (C) 2009 Martijn van der Kleijn <martijn.niji@gmail.com>
 * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
 *
 * This file is part of Wolf CMS.
 *
 * Wolf CMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wolf CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wolf CMS.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Wolf CMS has made an exception to the GNU General Public License for plugins.
 * See exception.txt for details and the full text.
*/

/* Security measure */
if (!defined('IN_CMS')) {
    exit();
}

/**
 * Provides Page not found page types.
 *
 * @package plugins
 * @subpackage page_not_found
 *
 * @author Philippe Archambault <philippe.archambault@gmail.com>
 * @copyright Philippe Archambault, 2008
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 *
 * @version $Id: index.php 187 2010-03-22 00:58:30Z martijn.niji@gmail.com $
 */

Plugin::setInfos(array(
        'id'          => 'page_not_found',
        'title'       => __('Page not found'),
        'description' => __('Provides Page not found page types.'),
        'version'     => '1.0.0',
        'website'     => 'http://www.wolfcms.org/',
        'update_url'  => 'http://www.wolfcms.org/plugin-versions.xml'
));

Behavior::add('page_not_found', '');
Observer::observe('page_not_found', 'behavior_page_not_found');

/**
 * Presents browser with a custom 404 page.
 */
function behavior_page_not_found() {
    $sql = 'SELECT * FROM '.TABLE_PREFIX."page WHERE behavior_id='page_not_found'";

    $stmt = Record::getConnection()->prepare($sql);
    $stmt->execute();

    $page = $stmt->fetchObject();

    if ($page) {
        $page = Page::find_page_by_uri($page->slug);

        if (is_object($page)) {
            header("HTTP/1.0 404 Not Found");
            header("Status: 404 Not Found");

            $page->_executeLayout();
            exit(); // need to exit otherwise true error page will be sent
        }
    }
}